# Maintainer: J. Peter Mugaas <jpmugaas@suddenlink.net>

_realname=uvatlas
_rn=UVAtlas
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
# Version from CMakeLists.txt
pkgver=1.9.0
_tag=mar2025
pkgrel=1
pkgdesc="UVAtlas Isochart Atlas Library (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://github.com/Microsoft/UVAtlas'
license=('spdx:MIT')
depends=("${MINGW_PACKAGE_PREFIX}-directx-headers"
         "${MINGW_PACKAGE_PREFIX}-directxmath"
         "${MINGW_PACKAGE_PREFIX}-directxmesh"
         "${MINGW_PACKAGE_PREFIX}-eigen3"
         "${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-omp"
         "${MINGW_PACKAGE_PREFIX}-spectra")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-directxtex")
source=("https://github.com/microsoft/UVAtlas/archive/${_tag}/${_realname}-${_tag}.tar.gz")
sha256sums=('af245b5cabc0766c7bb7f9e381dcca7547f7165290734f78ec54518b86565486')

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  if [[ "${MSYSTEM_CARCH}" == "aarch64" ]]; then
    extra_config+=("-DDIRECTX_ARCH=arm64")
  fi

  #DBUILD_DX11 requires fxc.exe but that's not available.
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    "${MINGW_PREFIX}"/bin/cmake.exe \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      -DBUILD_TOOLS=ON \
      -DENABLE_USE_EIGEN=ON \
      "${extra_config[@]}" \
      -DBUILD_SHARED_LIBS=ON \
      ../${_rn}-${_tag}

  "${MINGW_PREFIX}"/bin/cmake.exe --build .
}

check() {
  cd "${srcdir}/build-${MSYSTEM}"

  "${MINGW_PREFIX}"/bin/cmake.exe --build . --target test
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"

  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install .

  install -Dm644 "${srcdir}"/${_realname}-${_tag}/LICENSE \
    "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
}
